package com.wemei.auto.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.wemei.auto.ui.R;
import com.wemei.auto.ui.listener.OnValidationResultListener;

import java.util.Timer;
import java.util.TimerTask;

public class TimeButton extends androidx.appcompat.widget.AppCompatTextView implements View.OnClickListener, OnValidationResultListener {
    private static final String TAG = TimeButton.class.getSimpleName();
    private static String showText = "秒后重发";
    private static String NorText = "获取验证码";
    private Timer mTimer;
    private TimerTask mTimerTask;
    private long mTime;
    private long mLenght = 60000;
    private boolean isStart = false;
    private boolean defaultEnable = true;
    private int normalColor;
    private int hintColor;
    protected OnClickListener mOnClickListener;

    Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (mTime <= 0) {
                isStart = false;
                setTextColor(normalColor);
                TimeButton.this.setText(NorText);
            } else {
                TimeButton.this.setText(GetShowText());
            }
        }
    };

    public TimeButton(Context context) {
        this(context, null);
    }

    public TimeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTimeButton(attrs);
    }

    private void initTimeButton(AttributeSet attrs) {
        setText(NorText);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TimeButton);
        normalColor = a.getColor(R.styleable.TimeButton_time_button_normal, ContextCompat.getColor(getContext(), R.color.main_color));
        hintColor = a.getColor(R.styleable.TimeButton_time_button_enable_false, ContextCompat.getColor(getContext(), R.color.text_hint_color));
        defaultEnable = a.getBoolean(R.styleable.TimeButton_defaultEnable, true);
        setTextColor(defaultEnable ? normalColor : hintColor);
        a.recycle();
        setTextSize(12);
        setGravity(Gravity.CENTER);
        setOnClickListener(this);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        if (l != this) {
            this.mOnClickListener = l;
        } else {
            super.setOnClickListener(l);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnClickListener != null && !isStart && defaultEnable) {
            mOnClickListener.onClick(v);
        }
    }

    private void InitTimer() {
        mTime = mLenght;
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                mTime -= 1000;   //倒计时
                mHandler.sendEmptyMessage(0);
                if (mTime <= 0) {
                    ClearTimer();
                }
            }
        };
    }

    private void ClearTimer() {
        if (mTimerTask != null)
            mTimerTask.cancel();
        mTimerTask = null;

        if (mTimer != null)
            mTimer.cancel();
        mTimer = null;
    }

    public boolean enable(boolean enable) {
        if (enable && !defaultEnable) {
            setTextColor(normalColor);
        }
        if (!enable && defaultEnable) {
            setTextColor(hintColor);
        }
        defaultEnable = enable;
        return enable;
    }

    public boolean enable() {
        return defaultEnable;
    }

    public void start() {
        setTextColor(hintColor);
        InitTimer();
        isStart = true;
        mTimer.schedule(mTimerTask, 0, 1000);
    }

    public void SetTimeLenght(long lenght) {
        mLenght = lenght;
    }

    private String GetShowText() {
        if (mTime <= 0)
            return NorText;
        return mTime / 1000 + showText;
    }
}
